import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Op } from "sequelize";
import { getPageData, getPageWhere } from '@/utils/page';
import { RESPONSE_CODE } from '@/enums/code';
import { Slide } from './slide.model';
import { SlidePic } from './slidePic.model';
import { AddSlideDto, GetSlidePageDto, SlideOnOffDto } from './slide.dto';

@Injectable()
export class SlideService {
  constructor(
    @InjectModel(Slide)
    private slideModel: typeof Slide,

    @InjectModel(SlidePic)
    private slidePicModel: typeof SlidePic

  ) { }


  /**
   * 添加或更新slide
   * @method post 
   * @param addSlideDto 
   */
  async addOrUpdate(addSlideDto: AddSlideDto, username: string) {
    const { id, code, name, sysSlideShowPicList } = addSlideDto;
    const [instance, created] = await Slide.upsert(
      {
        id,
        code,
        name,
        number: sysSlideShowPicList.length,
        updateBy: username,
      },
      {
        conflictFields: ["id"],
      }
    );
    if (!created) {
      await this.slidePicModel.destroy({
        where: {
          slideId: id,
          [Op.not]: {
            id: sysSlideShowPicList.map((item: { id: number }) => item.id),
          },
        },
      });
      sysSlideShowPicList.map((item: any) => {
        this.slidePicModel.upsert({
          ...item,
          slideId: instance.dataValues.id,
        });
      });
    } else {
      await this.slidePicModel.bulkCreate(
        sysSlideShowPicList.map((item: any) => ({
          ...item,
          slideId: instance.dataValues.id,
        }))
      );
    }
  }

  /**
   * 获取slide分页数据
   * @method post 
   * @param getSlidePageDto 
   */
  async getSlidePage(getSlidePageDto: GetSlidePageDto) {
    const { current, size, code, name, status } = getSlidePageDto;
    const query: any = {};
    if (name) {
      query["name"] = {
        [Op.like]: `${name}%`,
      };
    }
    if (code) {
      query["code"] = {
        [Op.like]: `${code}%`,
      };
    }
    if (status !== undefined && status !== null) {
      query["status"] = status;
    }
    const { count, rows } = await this.slideModel.findAndCountAll({
      where: query,
      ...getPageWhere(current, size),
    });

    return {
      code: RESPONSE_CODE.SUCCESS,
      data: getPageData(current, size, count, rows),
      msg: null,
    }
  }

  /**
   * 更新slide状态
   * @method post 
   * @param slideOnOffDto 
   */
  async slideOnOff(slideOnOffDto: SlideOnOffDto) {
    const { id, status } = slideOnOffDto;
    await this.slideModel.update(
      { status },
      {
        where: { id },
      }
    );
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: null,
      msg: null,
    }
  }

  /**
   * 删除slide
   * @method post 
   * @param id 
   */
  async deleteSlide(id: number) {
    await this.slideModel.destroy({ where: { id } });
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: null,
      msg: null,
    }
  }

  /**
   * 获取slide详情
   * @method post 
   * @param id 
   */
  async getSlideInfo(id: number) {
    const slide = await this.slideModel.findByPk(id, {
      include: [{ model: this.slidePicModel, as: "sysSlideShowPicList" }],
    });
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: slide,
      msg: null,
    }
  }

  /**
   * 校验编码唯一性
   * @method post 
   * @param code 
   */
  async slideValidateCode(code: string) {
    const slide = await this.slideModel.findOne({
      where: { code },
    });
    if (slide) {
      return {
        code: RESPONSE_CODE.ERROR,
        data: [],
        msg: "编码已存在",
      }
    }
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: null,
      msg: null,
    }
  }
}
